<?php

App::uses('AppController', 'Controller');

/**
 * Users Controller
 *
 * @property User $User
 * @property PaginatorComponent $Paginator
 */
class UsersController extends AppController {

    public $layout = 'recargas';
    public $uses=array('User','Departamento','Zona');

    /**
     * Components
     *
     * @var array
     */
    public $components = array('Paginator');

    /**
     * index method
     *
     * @return void
     */
     public function beforeFilter() {
        parent::beforeFilter();
        //$this->Auth->allow();
    }
    public function index() {
        $usuarios =  $this->User->find('all');
        $this->set(compact('usuarios'));
    }
    

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->User->exists($id)) {
            throw new NotFoundException(__('Invalid user'));
        }
        $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
        $this->set('user', $this->User->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if($this->Session->read('Auth.User.role') == 'admin')
            {
                $role = 'regional';
            }
            if($this->Session->read('Auth.User.role') == 'regional')
            {
                $role = 'megadiler';
            }
            if($this->Session->read('Auth.User.role') == 'megadiler')
            {
                $role = 'diler';
            }
            
            
            $this->request->data['User']['role'] = $role;
            
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash('Se guardo con exito!!','msgbueno');
                if($this->Session->read('Auth.User.role') == 'admin')
                {
                    return $this->redirect(array('controller' => 'Users','action' => 'listadoregionales'));
                }
                if($this->Session->read('Auth.User.role') == 'regional')
                {
                    return $this->redirect(array('controller' => 'Users','action' => 'listadomegadilers'));
                }
                if($this->Session->read('Auth.User.role') == 'megadiler')
                {
                    return $this->redirect(array('controller' => 'Pedidos','action' => 'clientes'));
                }
                
            } else {
                $this->Session->setFlash('No se pudo guardar el usuario intente de nuevo!!!','msgerror');
            }
        }
     $departamento = $this->Departamento->find('list', array('fields'=>'Departamento.nombre'));
        $this->set(compact('departamento'));
        //debug($departamento);exit;
     $zona = $this->Zona->find('list', array('fields'=>'Zona.nombre'));
     $this->set(compact('zona'));
     //debug($zona);
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->User->exists($id)) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is(array('post', 'put'))) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
            $this->request->data = $this->User->find('first', $options);
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->User->delete()) {
            $this->Session->setFlash(__('The user has been deleted.'));
        } else {
            $this->Session->setFlash(__('The user could not be deleted. Please, try again.'));
        }
        return $this->redirect(array('action' => 'index'));
    }
     public function login() {
        $this->layout = 'login';
        
        if ($this->request->is('post')) {


            if ($this->Auth->login()) {
               
                $role = $this->Session->read('Auth.User.role');
                switch ($role){
                    case 'admin':$this->redirect(array('controller' => 'Users', 'action' => 'listadoregionales'));
                        break;
                    case 'regional':$this->redirect(array('controller' => 'Users', 'action' => 'listadomegadilers'));
                        break;
                    case 'megadiler': $this->redirect(array('controller' => 'Movimientos', 'action' => 'index'));
                        break;
                    case 'diler':$this->redirect(array('controller' => 'Clientes', 'action' => 'index'));
                        break;
                    case 'contador': $this->redirect(array('controller' => 'Contador', 'action' => 'general'));
                        break;
                }
            } else {
                $this->Session->setFlash('Usuario o Password incorrectos.','msgerror');
            }
        }
    }
    public function login222() {
        $this->layout = 'login';
        
        if ($this->request->is('post')) {


            if ($this->Auth->login()) {
                //$this->redirect($this->Auth->redirect());
                $role = $this->Session->read('Auth.User.role');
                if($role == 'admin')
                {                    
                    $this->redirect(array('controller' => 'Pedidos', 'action' => 'clientes'));
                }
                else{
                    $this->redirect(array('controller' => 'Clientes', 'action' => 'index'));
                }
                
                
            } else {
                $this->Session->setFlash('Usuario o Password incorrectos.','msgerror');
            }
        }
    }
    
    public function ajaxedit($idUsuario = null)
    {
        $this->layout = 'ajax';
        //debug($idUsuario);exit;
        if(!empty($this->request->data))
        {
            $this->User->id = $idUsuario;
            $this->User->save($this->request->data);
            $sw = true;
        }
        else{
            $this->request->data = $this->User->read(null,$idUsuario);
            
            $sw = false;
        }
        $this->set(compact('sw','idUsuario'));
        //debug($idUsuario);exit;
    }
    public function editapass()
    {
        $this->User->id = $this->Session->read('Auth.User.id');
        $this->User->save($this->request->data);
        $this->Session->setFlash('Se cambio correctamente el password!!!','msgbueno');
        $role = $this->Session->read('Auth.User.role');
        if($role == 'diler')
        {
            $this->redirect(array('controller' => 'Clientes', 'action' => 'index'));
        }
        if($role == 'admin')
        {
            $this->redirect(array('controller' => 'Users', 'action' => 'listadoregionales'));
        }
        
    }
    public function ajaxpass()
    {
        $this->layout = 'ajax';
    }
    public function ajaxcambiapass()
    {
        $this->layout = 'ajax';
    }
    public function salir()
    {
        //$this->Session->setFlash('Good-Bye');
        $this->redirect($this->Auth->logout());
        $this->redirect(array('action'=>'login'));
    }
    public function edita($idUsuario =null)
    {
        if(!empty($this->request->data))
        {
            $this->User->id = $idUsuario;
            $this->User->save($this->request->data);
            $this->Session->setFlash('Se edito Correctamente el usuario','msgbueno');
                if($this->Session->read('Auth.User.role') == 'admin')
                {
                    return $this->redirect(array('controller' => 'Users','action' => 'listadoregionales'));
                }
                if($this->Session->read('Auth.User.role') == 'regional')
                {
                    return $this->redirect(array('controller' => 'Users','action' => 'listadomegadilers'));
                }
                if($this->Session->read('Auth.User.role') == 'megadiler')
                {
                    return $this->redirect(array('controller' => 'Pedidos','action' => 'clientes'));
                }
            //$this->redirect(array('controller' => 'Pedidos', 'action' => 'clientes'));
        }
        else{
            $this->Session->setFlash('No se pudo!!!','msgerror');
            $this->redirect(array('controller' => 'Pedidos', 'action' => 'clientes'));
        }
    }
    public function listadoregionales()
    {
        $usuarios = $this->User->find('all',array('conditions' => array('User.role' => 'regional')));
        
        $this->set(compact('usuarios'));
        
    }
    public function listadomegadilers()
    {
        $usuarios = $this->User->find('all',array('conditions' => array('User.role' => 'megadiler')));
        
        $this->set(compact('usuarios'));
        
    }
    public function passmodal()
    {
        $this->layout = 'ajax';
    }
}
